package leetcode_201_300;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeeCode_228 {
    public static void main(String[] args) {
        System.out.println(summaryRanges(new int[]{0, 1, 2, 4, 5, 7, 8}));
        System.out.println(summaryRanges(new int[]{0,2,3,4,6,8,9}));
        System.out.println(summaryRanges(new int[]{1}));
    }
    private static List<String> summaryRanges(int[] nums) {
        Arrays.sort(nums);
        List<String> list = new ArrayList<>();
        if (nums.length == 0)
            return list;
        if (nums.length == 1){
            list.add(nums[0] + "");
            return list;
        }
        int start = nums[0];
        int pre = start;
        int end;
        for (int i = 0; i < nums.length; i++) {
            end = nums[i];
            if (end == pre){
                continue;
            }
            if (end - pre == 1){
                if (i == nums.length - 1){
                    list.add(start + "->" + end);
                    break;
                }
                pre = end;
                continue;
            }
            if (i == nums.length - 1){
                if (start == pre){
                    list.add(start + "");
                }else {
                    list.add(start + "->" + pre);
                }
                list.add(end + "");
                break;
            }
            if (start == pre){
                list.add(start + "");
            }else {
                list.add(start + "->" + pre);
            }
            start = nums[i];
            pre = start;
        }
        return list;
    }
}
